package com.dandelion.manager.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dandelion.commons.spring.SpringContextHolder;
import com.dandelion.commons.utils.StringUtils;
import com.dandelion.commons.web.model.AjaxResponse;
import com.dandelion.commons.web.model.DataGrid;
import com.dandelion.manager.system.domain.Permit;
import com.dandelion.manager.system.domain.RoleResource;
import com.dandelion.manager.system.repository.RoleResourceMapper;
import com.dandelion.manager.system.service.PermitService;
import com.dandelion.manager.system.service.RoleResourceService;

@Service
public class RoleResourceServiceImpl implements RoleResourceService {
	
	@Autowired
	private RoleResourceMapper mapper ;
	
	@Autowired
	private PermitService permitService ;

	@Override
	public AjaxResponse add(RoleResource record) {
		this.mapper.insertSelective(record) ;
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}

	@Override
	public AjaxResponse delete(long id) {
		this.mapper.deleteByPrimaryKey(id) ;
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}
	
	@Override
	public AjaxResponse deleteBatch(Long[] ids) {
		this.mapper.deleteByPrimaryKeys(ids) ;
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}


	@Override
	public AjaxResponse update(RoleResource record) {
		this.mapper.updateByPrimaryKeySelective(record) ;
		return SpringContextHolder.getBean(AjaxResponse.class).reset() ;
	}

	@Override
	public RoleResource get(long id) {
		return this.mapper.selectByPrimaryKey(id) ;
	}

	@Override
	public List<RoleResource> getAllList(RoleResource record) {
		return this.mapper.selectAllList(record);
	}
	
	@Override
	public DataGrid<RoleResource> datagrid(RoleResource conditions) {
		DataGrid<RoleResource> dg = new DataGrid<RoleResource>();

		Map<String, Object> conditionsMap = new HashMap<String, Object>();
		conditionsMap.put("record", conditions);
		conditionsMap.put("offset", dg.getOffset());
		conditionsMap.put("rows", dg.getPageSize());

		List<RoleResource> list = this.mapper.selectAllListPage(conditionsMap);
		int count = this.mapper.getCount(conditionsMap);
		dg.setRows(list);
		dg.setTotal(count);

		return dg;
	}

	@Override
	public void addOrUpdateRoleResource(Long roleId, Long[] resourceIds, String permitIds) {
		System.out.println(roleId);
		//删除该角色的所有资源
		this.delete(roleId);
		
		if(null != resourceIds && resourceIds.length > 0){//资源
			for(int i=0; i<resourceIds.length; i++) {
				RoleResource record = new RoleResource();
				record.setRole_id(roleId);
				record.setResource_id(resourceIds[i]);
				
				String[] permits = permitIds.split("\\|");
				record.setPermit_ids(permits[i]);
				
				this.add(record);
			}
		}
	}

	@Override
	public List<RoleResource> selectRoleResourceByRole(Long roleId) {
		List<RoleResource> rrVo = new ArrayList<RoleResource>();
		List<RoleResource> roleRes = this.mapper.selectRoleResourceByRole(roleId);
		
		for (RoleResource roleResource : roleRes) {
			List<Permit> permits = new ArrayList<Permit>() ;
			
			List<Permit> selectPermitByResource = this.permitService.selectPermitByResource(StringUtils.split(roleResource.getPermit_ids(), ",")) ;
			for (Permit permit : selectPermitByResource) {
				Permit p = new Permit() ;
				p.setId(permit.getId());
				p.setName(permit.getName());
				permits.add(p) ;
			}
			
			roleResource.setPermits(permits);
			rrVo.add(roleResource) ;
		}
		return rrVo ;
	}
	
}
